library(RNetLogo)
nl.path     <- "C:/Program Files (x86)/NetLogo 5.2.0"
model.path  <- "C:/Users/.../ABSAM_R.nlogo"

# initialize NetLogo
nl.LMA2.0R <- "nl.LMA2.0R"
NLStart(nl.path, gui=TRUE, nl.obj=nl.LMA2.0R)
NLLoadModel(model.path,nl.obj=nl.LMA2.0R)
  
  # an empty list to save the simulation results

  no.repeated.sim <- 20
  df1 <- NULL
  dfa1 <-NULL
  # repeated simulations 
  for (i in 1:no.repeated.sim)
  {
    # create a random-seed for NetLogo from R, based on min/max of NetLogo's random seed
    
    NLCommand("random-seed",runif(1,-2147483648,2147483647), nl.obj=nl.LMA2.0R)    
    NLCommand("setup", nl.obj=nl.LMA2.0R)
        
    # 12 months for start up
    
    NLDoCommand(1,"repeat 12 [go]", nl.obj=nl.LMA2.0R)
    
    dfa <- NLDoReport(144,"repeat 1 [go]",c("time", "unemployment-rate","ltu-rate", "theta", "unem", "otj", "emp-num", "ltu-prob", "js-prob",
                                          "js-almp-prob", "ltu-almp-prob", "agr-jobs", "serv-jobs", "prod-jobs", "wag", "prd", "ltu-prd",
                                          "ls-vacs", "ms-vacs", "hs-vacs", "dur", "ltu-dur", "free"),
                     
                           as.data.frame=T, df.col.names=c("t", "urate","lturate", "theta", "unem", "otj", "emp", "ltu_prob", "prob",
                                                           "almp_prob", "almp_pltu", "agr", "serv", "prod", "wag", "p", "ltup",
                                                           "lsv", "msv", "hsv", "dur", "ltudur", "free"), nl.obj=nl.LMA2.0R)
           
    dfa1 <- rbind(dfa1, dfa)
    
  }
 
  # return the mean of the repeated simulation results
require(data.table)
df1 <- data.table(df1)
df1 <- df1[,list(urate=mean(urate), lturate=mean(lturate), theta=mean(theta), unem=mean(unem), otj=mean(otj), emp=mean(emp),
                 ltu_prob=mean(ltu_prob), prob=mean(prob), almp_prob=mean(almp_prob), almp_pltu=mean(almp_pltu), agr=mean(agr),
                 serv=mean(serv), prod=mean(prod), wag=mean(wag), p=mean(p), ltup=mean(ltup), lsv=mean(lsv), msv=mean(msv), 
                 hsv=mean(hsv), dur=mean(dur), ltudur=mean(ltudur), free=mean(free)), by='t']


dfa1 <- data.table(dfa1)
dfa1 <- dfa1[,list(urate=mean(urate), lturate=mean(lturate), theta=mean(theta), unem=mean(unem), otj=mean(otj), emp=mean(emp),
                 ltu_prob=mean(ltu_prob), prob=mean(prob), almp_prob=mean(almp_prob), almp_pltu=mean(almp_pltu), agr=mean(agr),
                 serv=mean(serv), prod=mean(prod), wag=mean(wag), p=mean(p), ltup=mean(ltup), lsv=mean(lsv), msv=mean(msv), 
                 hsv=mean(hsv), dur=mean(dur), ltudur=mean(ltudur), free=mean(free)), by='t']



ccf(df1$unem, df1$free)






